package day52.pet.controller;

import day52.basic.domain.dto.AcceptOrderDto;
import day52.basic.util.CurrentLoginInfoUtil;
import day52.org.mapper.EmployeeMapper;
import day52.pet.service.ISearchMasterMsgService;
import day52.pet.domain.SearchMasterMsg;
import day52.pet.query.SearchMasterMsgQuery;
import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import day52.user.domain.Logininfo;
import day52.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Queue;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){

        if( searchMasterMsg.getId()!=null){
            searchMasterMsgService.update(searchMasterMsg);
        }else{
            searchMasterMsgService.add(searchMasterMsg);
        }
        return AjaxResult.me();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        searchMasterMsgService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id) {

        return searchMasterMsgService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {

        return searchMasterMsgService.pageQuery(query);
    }

    /**
     * 宠物寻主
     * @param msg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request) throws Exception {
        return searchMasterMsgService.publish(msg,request);
    }

    /**
     * 个人中心我发布的-用户可以看到自己所有发布的消息
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/own")
    public PageList<SearchMasterMsg> own(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) throws Exception {
        Logininfo lf = CurrentLoginInfoUtil.currentLoginInfo(request);
        Long userId = userMapper.findByLogininfoId(lf.getId()).getId();
        query.setUserId(userId);
        return searchMasterMsgService.pageQuery(query);
    }

    /**
     * 驳回-管理员看的
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/reject")
    public PageList<SearchMasterMsg> reject(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) throws Exception {
        query.setState(-1);
        return searchMasterMsgService.pageQuery(query);
    }

    /**
     * 寻主池-店铺可以看到所有寻主池的消息
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) throws Exception {
        query.setState(1);
        return searchMasterMsgService.pageQuery(query);
    }

    /**
     * 待处理-被分配的店铺可以看到自己店铺待处理的消息
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> handle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) throws Exception {
        Logininfo lf = CurrentLoginInfoUtil.currentLoginInfo(request);
        Long shopId = employeeMapper.findByLoginInfoId(lf.getId()).getShop_id();
        query.setShopId(shopId);
        query.setState(1);
        return searchMasterMsgService.pageQuery(query);
    }

    /**
     * 已完成-所有人都能看
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/complete")
    public PageList<SearchMasterMsg> complete(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) throws Exception {
        query.setState(2);
        return searchMasterMsgService.pageQuery(query);
    }

    /**
     * 拒绝接单
     * @param id
     * @return
     */
    @GetMapping("/rejectOrder/{id}")
    public AjaxResult rejectOrder(@PathVariable("id") Long id){
        return searchMasterMsgService.rejectOrder(id);
    }

    /**
     * 接受订单
     * @param dto
     * @return
     */
    @PostMapping("accept")
    public AjaxResult acceptOrder(@RequestBody AcceptOrderDto dto){
        return searchMasterMsgService.acceptOrder(dto);
    }
}
